package
{
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	
	import so.cuo.platform.mmedia.*;

	public class demo extends Sprite
	{
		public var bannerApid:String="";
		public var rectApid:String="";
		public var fullApid:String="";
			
		public var ui:UISprite; 
		public var xkey:TextField;
		public var ykey:TextField;
		public var adSizeTextField:TextField;
		private var mmedia:Mmedia;

		public function demo()
		{
			super();
			stage.align=StageAlign.TOP_LEFT;
			stage.scaleMode=StageScaleMode.NO_SCALE;
			initUI();
			mmedia=Mmedia.getInstance();
			if (mmedia.supportDevice)
			{
				mmedia.setInterstitialKeys(fullApid);
				mmedia.setBannerKeys(bannerApid);
				mmedia.addEventListener(MmediaEvent.onInterstitialReceive, onAdReceived);
				mmedia.addEventListener(MmediaEvent.onBannerReceive, onAdReceived);
				mmedia.enableTrace=true;
			}
		}
		
		private function initUI():void
		{
			ui=new UISprite(onClick);
			addChild(ui);
			xkey=ui.addButton("8",100,100);
			ykey=ui.addButton("0",300,100);
			adSizeTextField=ui.addButton("1",500,100);
			adSizeTextField.type=ykey.type=xkey.type=TextFieldType.INPUT;
			adSizeTextField.border=xkey.border=ykey.border=true;
			ui.addButton("absolute",100,250);
			ui.addButton("relation",240,250);
			ui.addButton("interstitial",320,250);
			ui.addButton("hide",460,250);
		}

		protected function onClick(label:String):void
		{
			if (!mmedia.supportDevice)
			{
				trace("not support device");
				return
			}
			else
			{
				var xv:int=parseInt(xkey.text);
				var yv:int=parseInt(ykey.text);
				var size:int=parseInt(adSizeTextField.text);
				var adsize:MmediaSize;
				if (size == 1)
				{
					adsize=Mmedia.BANNER;
				}
				if (size == 2)
				{
					adsize=Mmedia.IAB_BANNER;
				}
				if (size == 3)
				{
					adsize=Mmedia.IAB_MRECT;
				}
				if (size == 4)
				{
					adsize=Mmedia.IAB_LEADERBOARD;
				}
				if (adsize == null)
				{
					adsize=new MmediaSize(320, 50);
				}
				if (label =="hide")
				{
					mmedia.hideBanner();
				}
				if (label == "absolute")
				{
					mmedia.showBannerAbsolute(adsize, xv, yv);
				}
				else if (label =="relation")
				{
					if (xv > 9)
						xv=9;
					if (xv < 1)
						xv=1;
					mmedia.showBanner(adsize, xv);
				}
				else if (label =="interstitial")
				{
					var isReady:Boolean=mmedia.isInterstitialReady();
					if (isReady)
					{
						mmedia.showInterstitial();
					}
					else
					{
						mmedia.cacheInterstitial();
					}
				}
			}
		}

		protected function onAdReceived(event:*):void
		{
			if (event.type == MmediaEvent.onBannerReceive)
			{
				trace(event.data.width, event.data.height);
			}
			if (event.type == MmediaEvent.onInterstitialFailedReceive)
			{
				trace("------------",mmedia.isInterstitialReady());
			}
		}
	}
}
